package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.DestinationCommendQuery;
import cn.wolfcode.luowowo.article.service.IDestinationCommendService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.serach.service.IDestinationCommendSearchService;
import cn.wolfcode.luowowo.serach.service.IStrategySearchService;
import cn.wolfcode.luowowo.serach.template.DestinationCommendTemplate;
import cn.wolfcode.luowowo.serach.template.DestinationTemplate;
import cn.wolfcode.luowowo.serach.template.StrategyTemplate;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import static org.apache.zookeeper.Environment.list;

/**
 * Created by admin on 2019/8/17.
 */
@Controller
public class DataController {

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IDestinationCommendService destinationCommendService;

    @Reference
    private IDestinationCommendSearchService destinationCommendSearchService;

    @RequestMapping("init")
    @ResponseBody
    public Object dataInit() {

        /*List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            // 攻略主题初始化
            StrategyTemplate t = new StrategyTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());

           *//* List<String> list = strategyTagService.getTagsBySDetailId(detail.getId());
            t.setTags(list);*//*

            Destination dest = destinationService.getCountry(detail.getDest().getId());

            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            dest = destinationService.getProvince(detail.getDest().getId());
            if (dest != null) {
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }
            strategySearchService.saveOrUpdate(t);
        }*/

//        List<DestinationCommend> list = destinationCommendService.list();
//        for (DestinationCommend commend : list) {
//            DestinationCommendTemplate template = new DestinationCommendTemplate();
//            try {
//                BeanUtils.copyProperties(template,commend);
//                template.setDestId(commend.getDest().getId());
//                template.setDestName(commend.getDest().getName());
//                template.setDestThemeId(commend.getTheme().getId());
//                template.setDestThemeName(commend.getTheme().getName());
//                template.setDestThemeType(commend.getTheme().getType());
//                template.setDestThemeCoverUrl(commend.getCoverUrl());
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//            destinationCommendSearchService.saveOrUpdate(template);
//        }




        //舱位信息初始化-------------------------------------------------------------------------
        /*List<Cabininfo> cabininfos = cabininfoService.listAll();
        List<CabinInfoTemplate> cabinInfoTemplates = new ArrayList<>();
        for (Cabininfo cabininfo : cabininfos) {
            CabinInfoTemplate cabinInfoTemplate = new CabinInfoTemplate();
            try {
                BeanUtils.copyProperties(cabinInfoTemplate,cabininfo);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            cabinInfoTemplates.add(cabinInfoTemplate);
        }


        //航班初始化-------------------------------------------------------------------------

        List<Flight> fs = flightService.listAll();
        for (Flight f : fs) {
            FlightTemplate flightTemplate = new FlightTemplate();
            System.out.println("開始");
            try {
                BeanUtils.copyProperties(flightTemplate,f);
                flightTemplate.setFromCityCode(f.getFromCity().getAircode());
                flightTemplate.setFromCityName(f.getFromCity().getName());
                flightTemplate.setToCityCode(f.getToCity().getAircode());
                flightTemplate.setToCityName(f.getToCity().getName());

                long millisecond = f.getLandingTime().getTime() - f.getTakeoffTime().getTime();
                long second = millisecond / 1000;
                long minute = second / 60 % 60;
                long hour = second / 60 / 60;
                flightTemplate.setTimeDifference(hour+"小时"+minute+"分");

                flightTemplate.setCabinInfos(cabinInfoTemplates);
                flightTemplate.setBasePrice(cabinInfoTemplates.get(0).getNitTicketPrice().toString());

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            System.out.println(flightTemplate);
            flightSearchSeivice.saveOrUpdate(flightTemplate);
        }

        System.out.println("結束");*/





        return "ok";
    }
}